package com.xyzsoftware.commons.kernel.page;

import java.util.ArrayList;
import java.util.List;

import com.xyzsoftware.commons.kernel.Pagination;

/**
 * list page
 * @author Aspen
 */
public class Page<T> implements Pagination{
	
	/** 默认页大小  */
	public static final int DEFAULT_SIZE = 30;
	
	/** 
	 * 页内容
	 */
	private List<T> list = new ArrayList<T>();
		
	/**
	 * 总大小
	 */
	private int allSize = 0;
	
	/**
	 * 页大小
	 */
	private int size = 0;
	
	/**
	 * 当前页号
	 */
	private int number = 0;

	public Page(List<T> list, int allSize, int size, int number) {
		super();
		this.list = list;
		this.allSize = allSize;
		this.size = size;
		this.number = number;
	}
	
	public Page( List<T> list, int allSize ) {
		this( list,allSize, DEFAULT_SIZE,1 );
	}
	
	public Page( List<T> list ) {
		this( list,list==null?0:list.size(), DEFAULT_SIZE,1 );
	}
	
	public Page( int number ) {
		this( null,0, DEFAULT_SIZE,number );
	}
	

	/**
	 * 总页数
	 */
	public int getAllPage() {
		if( size < 0 )
			return (Math.max(0,allSize)+DEFAULT_SIZE-1)/DEFAULT_SIZE;
		return (Math.max(0,allSize)+size-1)/size;
	}
	
	/**
	 * 下一页号
	 */
	public int getNextNumber() {		
		return Math.min( Math.abs(number)+1,getAllPage() );
	}

	/**
	 * 上一页号
	 */
	public int getPreviousNumber() {
		return Math.max( 1, Math.abs(number)-1 );
	}

	/**
	 * 是否第一页
	 */
	public boolean isFirst() {
		return number <= 1;
	}

	/**
	 * 是否最后一页
	 */
	public boolean isLast() {
		return number >= getAllPage();
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public int getAllSize() {
		return allSize;
	}

	public void setAllSize(int allSize) {
		this.allSize = allSize;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}
	
}
